
<template>
  <div style="margin: 20px">
    <el-card>
      <el-header>上传政策文件</el-header>
      <div style="margin-bottom: 10px; display: flex">
        <el-form :model="dlgState.form" ref="dlg_form" label-position="left" label-width="90px">
          <el-form-item label="文件名称" prop="name" required>
            <el-input v-model="dlgState.form.fileName" style="width: 400px"
                      placeholder="例：国家发展改革委、住房城乡建设部关于调整部分行业建设项目财务基准收益率的通知"></el-input>
          </el-form-item>
          <el-form-item label="文件标签" prop="fileTags">
            <el-select multiple style="width: 150px;" placeholder="请选择标签" v-model="dlgState.form.fileTags">
              <el-option v-for="(item, index) in tagList" :key="index" :label="item.tagName"
                         :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item  label="文件" prop="file" required>
            <el-upload action="#" :auto-upload="false" :multiple="false" :file-list="fileList" :limit="1"
                       :on-change="changePic" :on-remove="removePic">
              <el-button type="primary">选择文件</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="发文部门" prop="releaseDepartment">
            <el-input v-model="dlgState.form.releaseDepartment"
                      placeholder="例：国家发展改革委、住房城乡建设部"></el-input>
          </el-form-item>
          <el-form-item label="发文年份" prop="releaseYear">
            <el-input v-model="dlgState.form.releaseYear" style="width: 220px" placeholder="例：2023"/>
          </el-form-item>
          <el-form-item label="发文号" prop="releaseNumber">
            <el-input v-model="dlgState.form.releaseNumber" style="width: 220px" placeholder="例：1号"/>
          </el-form-item>
          <el-form-item label="启用时间" prop="startTime" required>
            <el-input v-model="dlgState.form.startTime" style="width: 220px" placeholder="例：2000/01/01"/>
          </el-form-item>
          <el-form-item label="失效时间" prop="expireTime" required>
            <el-input v-model="dlgState.form.expireTime" style="width: 220px" placeholder="例：2001/01/01"/>
          </el-form-item>
          <el-button type="primary" @click="doAdd">
            上传
          </el-button>
        </el-form>

      </div>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import {reactive, ref} from "vue";
import policyTagApi from "@/api/basis/policyTag";
import policyFilesApi from "@/api/basis/policy";
import {ElMessage} from "element-plus";

const dlgState = reactive({
  form: {
    id: 0,
    fileTags: [],
    fileSource: "",
    releaseYear: "",
    releaseDepartment: "",
    releaseNumber: "",
    fileName: "",
    uploadTime: new Date(),
    startTime: "",
    expireTime: "",
    uploadUser: "",
    file: null,
    downloadCount: 0
  },
});
// 标签列表
const tagList = ref([] as Array<any>)
const fileList = ref([]);
const getTag = () => {
  policyTagApi.get().then((res) => {
    tagList.value = res.data.data
  })
}
const changePic = (file: any, fileList: any) => {
  dlgState.form.file = file.raw;
};
//删除文件
const removePic = () => {
  dlgState.form.file = null;
};
getTag();

const doAdd = () => {
  let temp = JSON.stringify(dlgState.form.fileTags);
  temp = temp.substring(1, temp.length -1)
  policyFilesApi
      .add({
        id: 0,
        fileTags: temp,
        fileSource: dlgState.form.fileSource,
        releaseYear: dlgState.form.releaseYear,
        releaseDepartment: dlgState.form.releaseDepartment,
        releaseNumber: dlgState.form.releaseNumber,
        fileName: dlgState.form.fileName,
        startTime: dlgState.form.startTime,
        expireTime: dlgState.form.expireTime,
        censorStat: 0,
        downloadCount: 0,
        file: dlgState.form.file
      })
      .then((res) => {
        if (res.status === 200) {
          if (res.data.code === 500) {
            ElMessage.error(res.data.message);
          } else {
            ElMessage.success("操作成功");
          }
        }else {
          ElMessage.error("操作失败")
        }
      });
};

</script>
<style scoped lang="scss">

</style>